﻿using Microsoft.EntityFrameworkCore;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using CRM.Models.Email;

namespace CRM.Models
{
    [Table("customer")]
    [Index(nameof(CompanyId), Name = "CompanyId")]
    [Index(nameof(OemId), Name = "OemId")]
    [Index(nameof(UserId), Name = "UserId")]
    [Index(nameof(UserGroupId), Name = "UserGroupId")]
    [Index(nameof(DealStatus), Name = "DealStatus")]
    [Index(nameof(Email), Name = "Email")]
    [Index(nameof(Mobile), Name = "Mobile")]
    [Index(nameof(BelongUserId), Name = "BelongUserId")]
    [Index(nameof(IsPrivate), Name = "IsPrivate")]
    [Index(nameof(IsIntention), Name = "IsIntention")]
    [Index(nameof(GroupId), Name = "DiyGroupId")]
    [Index(nameof(SourceId), Name = "SourceId")]
    [Index(nameof(OemId), nameof(CompanyId))]
    public class Customer : BaseModel
    {
        public int Id { get; set; }

        [Column(TypeName = "varchar(255)"), Comment("客户名称")]
        public string Name { get; set; }

        public int OemId { get; set; }
        public int CompanyId { get; set; }
        public int UserGroupId { get; set; }
        public int UserId { get; set; }

        [Comment("手机号")]
        public string Mobile { get; set; }

        [Column(TypeName = "varchar(255)"), Comment("公司邮箱")]
        public string Email { get; set; }

        [Column(TypeName = "varchar(255)"), Comment("个人邮箱")]
        public string PersonalEmail { get; set; } = string.Empty;

#nullable enable
        [Column(TypeName = "varchar(50)"), Comment("领英网址")]
        public string? LinkedinUrl { get; set; }

        [Column(TypeName = "varchar(255)"), Comment("职称")]
        public string? JobTitle { get; set; }

        [Column(TypeName = "varchar(50)"), Comment("公司电话")]
        public string? CompanyTel { get; set; }
#nullable restore

        [Comment("成交状态 0未成交 1成交")]
        public int DealStatus { get; set; } = 0;

        [Comment("归属人")]
        public int BelongUserId { get; set; } = 0;

        [Comment("获取客户时间")]
        [Column(TypeName = "timestamp")]
        public DateTime? BelongAt { get; set; }

        [Comment("最后跟进时间")]
        [Column(TypeName = "timestamp")]
#nullable enable
        public DateTime? FollowAt { get; set; }


        [Comment("最后跟进记录")]
        public string? FollowMessage { get; set; }
#nullable restore

        [Comment("公海(私人) 0公海 1私人")]
        public int IsPrivate { get; set; } = 1;

        [Comment("意向 0无 1有")]
        public int IsIntention { get; set; } = 0;

        [Comment("关注度")]
        public int Attention { get; set; } = 0;

        public string Remarks { get; set; }


        [Comment("类型 1全球搜索 2数据营销 3外部导入")]
        public int Type { get; set; }

#nullable enable
        [Comment("分组")]
        public int? GroupId { get; set; }

        [Comment("来源")]
        public int? SourceId { get; set; } = 0;


        public CustomerGroup? Group { get; set; }
        public CustomerSource? Source { get; set; }
        public List<CustomerFollow>? Follows { get; set; }
        public virtual List<CustomerLabel>? Labels { get; set; }

        public User? User { get; set; }
        [ForeignKey("BelongUserId")]
        public User? BelongUser { get; set; }
#nullable restore

        [Description("块主键id")]
        public int BlockId { get; set; } = 1;

        [Description("块主键id")]
        public CustomerCompany Block { get; set; }

        [NotMapped]
        [Description("1 我的客户添加 2意向客户添加")]
        public int Origin { get; set; } = 1;

        public enum DealStatusEnum
        {
            [Description("未成交")]
            Unsettled,
            [Description("已成交")]
            Closed,
        }

        public enum IsIntentionEnum
        {
            无,
            有
        }
        public Collection<EmailMarketing> Marketing { get; set; }
    }
}
